package exercise;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Exercise {
    public static void main(String[] args) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/exercise/books.xml"));
        Element rootElement = document.getRootElement();
        List<Element> xmlBooks = rootElement.elements("book");
        List<Book> books = new ArrayList<>();
        for (Element bookElement : xmlBooks) {
            String id = bookElement.attributeValue("id");
            String name = bookElement.element("name").getText();
            String author = bookElement.element("author").getText();
            String price = bookElement.element("price").getText();
            Book book = new Book(id,name,author,Float.parseFloat(price));
            books.add(book);
        }
        for (Book book : books) {
            System.out.println(book);
        }
    }
}
